package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.UpLoadControllerApi;
import com.xuecheng.api.media.model.upload.TokenResult;
import com.xuecheng.api.media.model.upload.VodRequest;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author 黄子弟
 * @Date 2021/12/14
 */

@Slf4j
@RestController
public class UpLoadController implements UpLoadControllerApi {

    @Value("${aliyun.region}")
    private String regionId;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    /**
     *获取上传凭证
     * @param vodRequest  请求信息封装类
     * @return TokenResult 凭证信息封装类
     */
    @Override
    @PostMapping("/media/vod-token")
    public TokenResult getUpLoadToken(@RequestBody VodRequest vodRequest) {
        //获取客户端对象
        try {
            //调用第三方，有异常用try-catch
            DefaultAcsClient client = AliyunVODUtil.initVodClient(regionId, accessKeyId, accessKeySecret);
            CreateUploadVideoResponse response = AliyunVODUtil.createUploadVideo(client, vodRequest.getTitle(), vodRequest.getFileName());
            //解析返回对象，封装到TokenResult类
            TokenResult tokenResult = new TokenResult();
            String requestId = response.getRequestId();
            String uploadAddress = response.getUploadAddress();
            String videoId = response.getVideoId();
            String uploadAuth = response.getUploadAuth();
            tokenResult.setRequestId(requestId);
            tokenResult.setUploadAddress(uploadAddress);
            tokenResult.setUploadAuth(uploadAuth);
            tokenResult.setVideoId(videoId);

            return tokenResult;
        } catch (Exception e) {
            log.error(MediaErrorCode.E_140011.getDesc()+e.getMessage());
            ExceptionCast.cast(CommonErrorCode.UNKOWN);
            return null;

        }
    }

    /**
     *刷新上传凭证
     * @param videoId  视频标识id
     * @return  TokenResult 凭证信息封装类
     */
    @Override
    @GetMapping("/media/refresh-vod-token/{videoId}")
    public TokenResult refreshUpLoadToken(@PathVariable String videoId) {
        //获取客户端对象
        try {
            //调用第三方，有异常用try-catch
            DefaultAcsClient client = AliyunVODUtil.initVodClient(regionId, accessKeyId, accessKeySecret);
            RefreshUploadVideoResponse response = AliyunVODUtil.refreshUploadVideo(client, videoId);
            //解析返回对象，封装到TokenResult类
            TokenResult tokenResult = new TokenResult();
            String requestId = response.getRequestId();
            String uploadAddress = response.getUploadAddress();
            String uploadAuth = response.getUploadAuth();
            tokenResult.setRequestId(requestId);
            tokenResult.setUploadAddress(uploadAddress);
            tokenResult.setUploadAuth(uploadAuth);
            tokenResult.setVideoId(response.getVideoId());

            return tokenResult;
        } catch (Exception e) {
            log.error(MediaErrorCode.E_140011.getDesc()+e.getMessage());
            ExceptionCast.cast(CommonErrorCode.UNKOWN);
            return null;
        }
    }
}
